from sqlalchemy import Column, Integer, String, Float, Enum, ForeignKey, DateTime, Text
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from datetime import datetime

from app.database import Base


class Algorithm(Base):
    """算法模型
    
    存储检测算法信息
    """
    __tablename__ = "algorithms"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(50), nullable=False)
    description = Column(String(255))
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    tasks = relationship("DetectionTask", back_populates="algorithm")


class Dataset(Base):
    """数据集模型
    
    存储上传的数据集信息
    """
    __tablename__ = "datasets"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    file_path = Column(String(255), nullable=False)
    record_count = Column(Integer, default=0)
    feature_count = Column(Integer, default=0)
    class_count = Column(Integer, default=0)
    user_id = Column(Integer, ForeignKey("users.id"))
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    user = relationship("User", back_populates="datasets")
    tasks = relationship("DetectionTask", back_populates="dataset")


class DetectionTask(Base):
    """检测任务模型
    
    存储检测任务信息
    """
    __tablename__ = "detection_tasks"
    
    id = Column(Integer, primary_key=True, index=True)
    algorithm_id = Column(Integer, ForeignKey("algorithms.id"), nullable=False)
    dataset_id = Column(Integer, ForeignKey("datasets.id"), nullable=False)
    status = Column(Enum("pending", "running", "completed", "failed"), default="pending")
    accuracy = Column(Float(precision=2))
    recall = Column(Float(precision=2))
    loss = Column(Float(precision=2))
    user_id = Column(Integer, ForeignKey("users.id"))
    start_time = Column(DateTime(timezone=True))
    end_time = Column(DateTime(timezone=True))
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    algorithm = relationship("Algorithm", back_populates="tasks")
    dataset = relationship("Dataset", back_populates="tasks")
    user = relationship("User", back_populates="tasks")
    results = relationship("DetectionResult", back_populates="task")


class DetectionResult(Base):
    """检测结果模型
    
    存储检测统计结果
    """
    __tablename__ = "detection_results"
    
    id = Column(Integer, primary_key=True, index=True)
    task_id = Column(Integer, ForeignKey("detection_tasks.id"), nullable=False)
    result_type = Column(String(20), nullable=False)  # traffic, attack, protocol, flow
    category = Column(String(50), nullable=False)     # HTTP, DDoS, TCP等
    value = Column(Integer, nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    task = relationship("DetectionTask", back_populates="results") 